package com.feeyo.groundservice.base_util.upgrade

import android.util.TypedValue
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.ActivityUtils
import com.feeyo.groundservice.base_util.FyLog
import com.feeyo.groundservice.base_util.R
import com.feeyo.groundservice.base_util.getColor
import com.feeyo.groundservice.base_util.getPxFromResource
import com.feeyo.groundservice.base_util.toJson
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.impl.ConfirmPopupView
import com.lxj.xpopup.interfaces.OnConfirmListener
import com.lxj.xpopup.interfaces.SimpleCallback

/**
 *作者：QLH on 2021-04-20
 *描述：版本升级
 * 注意：需要再onStart周期前初始化
 */
class UpgradeUtil(activity: AppCompatActivity) {
    private val TAG = javaClass.simpleName

    private val upgradeManagerUtil = UpgradeManagerUtil(activity)

    fun checkUpdate(info: UpgradeInfo) {
        FyLog.e(TAG, info.toJson())
        val context = ActivityUtils.getTopActivity() ?: return
        XPopup.Builder(context)
            .dismissOnTouchOutside(false)
            .isDestroyOnDismiss(true)
            .dismissOnBackPressed(false)
            .setPopupCallback(object : SimpleCallback() {
                override fun beforeShow(popupView: BasePopupView?) {
                    super.beforeShow(popupView)
                    //反射定制属性
                    (popupView as ConfirmPopupView).let {
                        val tv_title = it.javaClass.getDeclaredField("tv_title")
                        val tv_content = it.javaClass.getDeclaredField("tv_content")
                        val tv_cancel = it.javaClass.getDeclaredField("tv_cancel")
                        val tv_confirm = it.javaClass.getDeclaredField("tv_confirm")

                        tv_confirm.isAccessible = true
                        tv_cancel.isAccessible = true
                        tv_content.isAccessible = true
                        tv_title.isAccessible = true

                        (tv_confirm.get(it) as TextView).apply {
                            setTextColor(R.color.blue_main_2E76EE.getColor())
                            setTextSize(
                                TypedValue.COMPLEX_UNIT_PX,
                                R.dimen.d14.getPxFromResource().toFloat()
                            )
                        }
                        (tv_cancel.get(it) as TextView).apply {
                            setTextColor(R.color.black_66.getColor())
                            setTextSize(
                                TypedValue.COMPLEX_UNIT_PX,
                                R.dimen.d14.getPxFromResource().toFloat()
                            )
                        }
                        (tv_title.get(it) as TextView).apply {
                            setTextColor(R.color.black_27.getColor())
                            setTextSize(
                                TypedValue.COMPLEX_UNIT_PX,
                                R.dimen.d20.getPxFromResource().toFloat()
                            )
                        }
                        (tv_content.get(it) as TextView).apply {
                            setTextColor(R.color.black_66.getColor())
                            setTextSize(
                                TypedValue.COMPLEX_UNIT_PX,
                                R.dimen.d16.getPxFromResource().toFloat()
                            )
                        }
                    }
                }
            })
            .asConfirm(info.title, info.content, "下次再说", "立即更新", OnConfirmListener {
                upgradeManagerUtil.checkUpdate(info.url)
            }, null, info.isForceUpgrade, R.layout.upgrade_confirm).show()
    }
}